﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.wMall.Buyer
{
    public partial class wfBindBalanceInfo : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CustIsSetInnerPayPwd();
            }
        }

        private void CustIsSetInnerPayPwd()
        {
            using (msBsc.AppLogic.DataAlc.CBizCustomerRegister objCBizCustomerRegister = new AppLogic.DataAlc.CBizCustomerRegister())
            {
                System.Data.DataSet ds = objCBizCustomerRegister.DataObj.QueryBySQL(@"SELECT CUSTOMER_PAY_INNERPWD FROM DJK_CUSTOMER_REGISTER WHERE CUSTOMER_REGISTER_ID='" + msBsc.SvRef.UserInfo.Operator + "'");

                if (ds == null
                    || ds.Tables[0].Rows.Count <= 0)
                {
                    this.ExecJScript(@"alert(""当前无账号登陆，请登陆"");window.location.href='/wp_UserLogin.htm'");
                    return;
                }

                if (ds.Tables[0].Rows[0]["CUSTOMER_PAY_INNERPWD"] == DBNull.Value
                    || string.IsNullOrEmpty(ds.Tables[0].Rows[0]["CUSTOMER_PAY_INNERPWD"].ToString()))
                {
                    this.ExecJScript(@"alert(""您尚未设置支付密码。请设置支付密码后，再进行支付账号绑定。"");window.location.href=""/wb_SetInnerPayPwd.htm?ReturnUrl=/wb_BindBalanceInfo.htm""");
                    return;
                }

                ConfirmInnerParPwd();
            }
        }

        private void ConfirmInnerParPwd()
        {
            this.ExecJScript(@"$(""input,select,button"").attr(""disabled"",""disabled"");$(""#pwdInnderPwd"").removeAttr(""disabled"")");
            if (!msBsc.SvRef.UserInfo.UsrIsCmfInnerPwd)
            {
                this.ExecJScript(@"window.location.href=""wb_CmfInnerPayPwd.htm?ReturnUrl=/wb_BindBalanceInfo.htm""");
                return;
            }

            using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo objCBizCustomerBaseInfo = new AppLogic.DataAlc.CBizCustomerBaseInfo())
            {
                System.Data.DataSet ds = objCBizCustomerBaseInfo.SelectById(msBsc.SvRef.UserInfo.Operator);

                DateTime clearDt;

                if (DateTime.TryParse(ds.Tables[0].Rows[0]["CUSTOMER_CLEAR_ALIPAY_DT"].ToString(), out clearDt))
                {
                    TimeSpan tis = objCBizCustomerBaseInfo.dtCurrDateTime - clearDt;

                    if (tis.Days < 7)
                    {
                        this.ExecJScript(@"alert(""您的支付宝账户，距离上次解绑不足7天。请" + (7 - tis.Days) + @"天后，再进行绑定。"");window.location.href=""/wb_TouristInfo.htm""");
                        return;
                    }
                }
            }

            this.ExecJScript(@"$(""input,select,button"").removeAttr(""disabled"",""disabled"")");
            InitCustBalanceInfo();
        }

        private void InitCustBalanceInfo()
        {
            DataSet ds;
            //            DataSet ds = new msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo().DataObj.QueryBySQL(@"SELECT * FROM BSE_BANK ORDER BY SEQ_NUM");

            //            var countryJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 200, "");
            //            this.ExecJScript(@"var _html = $('#bank_name_list').html();
            //                            laytpl(_html).render(" + countryJson + @", function (render) {
            //                                $('#selBankList').html(render);
            //                            });");

            ds = new msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo().DataObj.QueryBySQL(@"SELECT * FROM DJK_CUSTOMER_BASEINFO WHERE CUSTOMER_BASEINFO_ID='" + msBsc.SvRef.UserInfo.Operator + "'");

            if (ds == null || ds.Tables[0].Rows.Count <= 0)
            {
                return;
            }

            //if (ds.Tables[0].Rows[0]["CUSTOMER_BANK_TYPE"] != DBNull.Value
            //    && ds.Tables[0].Rows[0]["CUSTOMER_BANK_TYPE"].ToString() != "")
            //{
            //    this.ExecJScript(@"InitBankInfo('" + ds.Tables[0].Rows[0]["CUSTOMER_BANK_TYPE"].ToString() + "','" + ds.Tables[0].Rows[0]["CUSTOMER_ACCOUNT_NAME"].ToString() + "','" + ds.Tables[0].Rows[0]["CUSTOMER_BANK_ACCOUNT"].ToString() + "','" + ds.Tables[0].Rows[0]["CUSTOMER_BANK_NAME"].ToString() + "')");
            //}

            if (ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"] != DBNull.Value
                && ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"].ToString() != "")
            {
                this.ExecJScript(@"InitAliPayInfo('" + ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"].ToString() + "')");
            }

        }

        public override void DoAction()
        {
            if (this.strAction == "BindBankInfo")
            {
                BindBankInfo();
            }
            if (this.strAction == "BindAlipayInfo")
            {
                BindAliPayInfo();
            }
        }

        private void BindBankInfo()
        {
            bool isModifyBank = this.GetParamHtValue("IsModifyBank") == "Y";
            string bankId = this.GetParamHtValue("bankId");
            if (string.IsNullOrEmpty(bankId) || bankId == "0")
            {
                this.ExecJScript(@"ShowErrMsg(""selBankList"", ""notice-msg-bank"", ""请选择银行账户开户行"");");
                return;
            }

            string busername = this.GetParamHtValue("busername");
            if (string.IsNullOrEmpty(busername))
            {
                this.ExecJScript(@"ShowErrMsg(""txtBankUserNme"", ""notice-msg-username"", ""银行账户用户名不能为空"");");
                return;
            }

            string baccount = this.GetParamHtValue("baccount");
            if (string.IsNullOrEmpty(baccount))
            {
                this.ExecJScript(@"ShowErrMsg(""txtBankAccount"", ""notice-msg-bankaccount"", ""银行账户不能为空"");");
                return;
            }

            string bcomnme = this.GetParamHtValue("bcomnme");
            if (string.IsNullOrEmpty(bcomnme))
            {
                this.ExecJScript(@"ShowErrMsg(""txtBankComNme"", ""notice-msg-comnme"", ""开户行全称不能为空"");");
                return;
            }

            using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo objCBizCustomerBaseInfo = new AppLogic.DataAlc.CBizCustomerBaseInfo())
            {
                string resultStr = objCBizCustomerBaseInfo.BindBankInfo(bankId, busername, baccount, bcomnme, isModifyBank);
                if (string.IsNullOrEmpty(resultStr))
                {
                    this.ExecJScript("alert('绑定银行信息成功!');window.location.href='/wb_BindBalanceInfo.htm'");
                    return;
                }
                this.ExecJScript(resultStr);
            }
        }

        private void BindAliPayInfo()
        {
            string alipayAccount = this.GetParamHtValue("alipayAccount");
            bool isModifyAlipay = this.GetParamHtValue("IsModifyAlipay") == "Y";

            if (isModifyAlipay)
            {
                this.Alert("操作有误，不能对账号进行修改，请解绑后重新绑定！");
                return;
            }

            if (string.IsNullOrEmpty(alipayAccount))
            {
                this.ExecJScript(@"ShowErrMsg(""txtAlipayAccount"", ""notice-msg-aliaccount"", ""支付宝账号不能为空"");");
                return;
            }

            using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo objCBizCustomerBaseInfo = new AppLogic.DataAlc.CBizCustomerBaseInfo())
            {
                string resultStr = objCBizCustomerBaseInfo.BindAliPayInfo(alipayAccount, isModifyAlipay);
                if (string.IsNullOrEmpty(resultStr))
                {
                    this.ExecJScript("alert('绑定支付宝账号成功!您可以使用该支付宝账户进行提现！');window.location.href='/wb_TouristInfo.htm'");
                    return;
                }
                this.ExecJScript(resultStr);
            }
        }
    }
}